package com.ruoyi.ora.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.dict.OrderStatus;
import com.ruoyi.common.enums.dict.OrderType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ora.domain.MemberLevel;
import com.ruoyi.ora.domain.Order;
import com.ruoyi.ora.mapper.OrderMapper;
import com.ruoyi.ora.service.IMemberLevelService;
import com.ruoyi.ora.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单Service业务层处理
 * 
 * @author liuwp
 * @date 2024-11-14
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IMemberLevelService memberLevelService;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param order 订单
     * @return 订单
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 根据业务主键查询一条记录：订单
     *
     * @param order 订单
     * @return 订单
     */
    @Override
    public Order selectOneOrder(Order order)
    {
        return orderMapper.selectOneOrder(order);
    }

    /**
     * 新增订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单
     * 
     * @param order 订单
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        return orderMapper.updateOrder(order);
    }

    @Override
    public int updateOrderCondition(Order order, Order orderCondition)
    {
        return orderMapper.updateOrderCondition(order, orderCondition);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }

    /**
     * 条件删除订单
     *
     * @param order 订单
     * @return 结果
     */
    @Override
    public int deleteOrderByCondition(Order order)
    {
        return orderMapper.deleteOrderByCondition(order);
    }

    @Override
    public AjaxResult appBuildOrder(Order order) {
        // 默认订单类型   //TODO   只有一个VIP购买

        order.setOrderType(OrderType.VIPBUY.getValue());
        return buildOrderVipbuy(order);
//        if (order.getOrderType() == null) order.setOrderType(OrderType.VIPBUY.getValue());
//
//        if (order.getOrderType().intValue() == OrderType.VIPBUY.getValue()){
//            return buildOrderVipbuy(order);
//        }else if (order.getOrderType().intValue() == OrderType.RECHARGE.getValue()){
//            return null;
//        }
         // TODO 订单类型错误
//        return AjaxResult.error("订单类型错误");
    }

    public AjaxResult buildOrderVipbuy(Order order) {
        // app 下单  VIP 够买
        try {
            if (order.getProductId() != null){
                MemberLevel memberLevel = memberLevelService.selectOneMemberLevel(Builder.of(MemberLevel::new)
                        .with(MemberLevel::setId, order.getProductId())
                        .with(MemberLevel::setLang, order.getLang())
                        .build());
                if (memberLevel != null){
                    order.setOrderTitle(memberLevel.getMemberLevelDesc().getLevelName());
                    order.setOrderNo("A"+IdUtils.getCheckCode(15, true));
                    order.setOrderAmount(memberLevel.getLevelFee());
                    order.setStatus(OrderStatus.CREATE.getValue());
                    order.setSerialNumber("000"+IdUtils.getCheckCode(15, true));
                    order.setUserId(SecurityUtils.getUserId());
                    order.setCreateTime(new Date());
                    // TODO 添加产品id字段，代码用户购买的什么会员等级
                    order.setProductId(memberLevel.getId());

//                    order.setPayChannel();   // TODO 支付通道  1 apple 2 google

                    insertOrder(order);
                    // TODO 根据支付通道获取支付参数

                    return AjaxResult.success(order);
                }else {
                    // TODO  转多语言
                    return AjaxResult.error("等级数据获取失败");
                }
            }else {
                return AjaxResult.error(MessageUtils.message("ora.common.required"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

}
